﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdMaterial
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 8:33:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BProdMaterial : IProdMaterial
{
    public List<POProdMaterial> GetMaterialByCodes(List<string> materialCodes)
    {
        var db = Dao.Db.Default;
        var result = db.ProdMaterials
            .Where(t => materialCodes.Contains(t.MaterialCode)).ToList();
        return result;
    }
    public POProdMaterial CreateMaterial(string materialCode, string materialName)
    {
        var result = new POProdMaterial
        {
            Id = ConstFields.NoRead.Equals(materialCode) ? 1 : ConstFields.NONE.Equals(materialCode) ? 2 : XID.Nextval,
            Spec = materialCode,
            MaterialCode = materialCode,
            MaterialName = materialName,
        };

        var db = Db.Default;

        db.ProdMaterials.Insert(result);
        return result;
    }
    public POProdMaterial AddProdMaterial(TireInfo tireInfo)
    {
        var db = Db.Default;
        var material = db.ProdMaterials.Where(f => f.Spec == tireInfo.MaterialCode).FirstOrDefault();

        if (material is not null)
        {
            return material;
        }
        var bean = new POProdMaterial()
        {
            Id = XID.Nextval,
            MaterialCode = tireInfo.MaterialCode,
            MaterialName = tireInfo.MaterialDesc,
            MaterialHeight = tireInfo.Height,
            InsideDiameter = Convert.ToDecimal(tireInfo.Size),
            Spec = tireInfo.MaterialCode,
        };

        var s=db.ProdMaterials.Insert(bean);
        //db.SaveChanges();
        return bean;
    }
    public POProdMaterial GetMaterialByCode(string materialCode)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => t.MaterialCode == materialCode).FirstOrDefault();

        return result;
    }
    public POWcsMaterialRatio GetMaterialRatioByMaterialId(long materialId)
    {
        var db = Db.Default;
        var result = db.WcsMaterialRatios.Where(t => t.MaterialId == materialId).FirstOrDefault();
        return result;
    }

    public POProdMaterial GetMaterialBySpec(string spec)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => t.Spec == spec).FirstOrDefault();

        return result;
    }


    public List<MaterialCodeBean> GetMaterialCodeAndId()
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Select(t => new MaterialCodeBean
            {
                MaterialCode = t.MaterialCode,
                MaterialId = t.Id,
            }).ToList();

        return result;
    }

    public List<POProdMaterial> GetMaterialBySpecs(List<string> specs)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => specs.Contains(t.Spec)).ToList();

        return result;
    }

    public POProdMaterial GetMaterialById(long materialId)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => t.Id == materialId).FirstOrDefault();

        return result;
    }
    public int UpdateMaterialScanNumById(POProdMaterial material)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => t.Id == material.Id).Update(new POProdMaterial
            {
                ScanNum = material.ScanNum
            });

        return db.SaveChanges();
    }

    public int UpdateMaterialHeightByCode(string materialCode,string height )
    {
        //var db = Db.Default;
        //var result = db.ProdMaterials
        //    .Where(t => t.MaterialCode == materialCode).Update(new POProdMaterial
        //    {
        //        MaterialHeight = decimal.Parse(height)
        //    });

        //return db.SaveChanges();

        return 0;
    }

    public POProdMaterial GetMaterialBySkuId(long skuId)
    {
        var db = Db.Default;
        var result = db.ProdMaterials.Join(db.ProdSkus, f => f.Id, s => s.MaterialId, (f, s) => new
        {
            Material = f,
            SkuId = s.Id,
        })
            .Where(t => t.SkuId == skuId).Select(t => t.Material).FirstOrDefault();

        return result;
    }

    public POProdMaterial GetMaterialByMesId(string mesId)
    {
        var db = Db.Default;
        var result = db.ProdMaterials
            .Where(t => t.MesId == mesId).FirstOrDefault();

        return result;
    }

    public int RefreshMaterial(MaterialMDCInfo material)
    {
        if (material is null)
        {
            return 0;
        }
        if (material.MaterialCode is null)
        {
            return 0;
        }

        var db = Db.Default;
        var id = XID.Nextval;
        var id2 = XID.Nextval;


        var materialMaster = GetMaterialByCode(material.MaterialCode);
        var materialInfo = db.ProdMaterialInfos.Where(r => r.MaterialCode == material.MaterialCode).FirstOrDefault();

        var prodMaterial = new POProdMaterial()
        {
            MesId = material.MaterialId,
            Spec = material.MaterialCode,
            MaterialCode = material.MaterialCode,
            MaterialName = material.MaterialName,

            MaterialInch = material.MaterialInch,
            InsideDiameter = material.InsideDiameter,
            OutsideDiameter = material.OutsideDiameter,
            MaterialWeight = material.TireWeight,
            MaterialHeight = material.MaterialHeight,

            WinMax = material.Winmax,
            WinMin = material.Winmin,
            SumMax = material.Summax,
            SumMin = material.Summin,
        };

        var prodMaterialInfo = new POProdMaterialInfo()
        {
            MesId = material.MaterialId,
            Spec = material.MaterialCode,
            MaterialCode = material.MaterialCode,
            MaterialName = material.MaterialName,

            MaterialInch = material.MaterialInch,
            InsideDiameter = material.InsideDiameter,
            OutsideDiameter = material.OutsideDiameter,
            MaterialWeight = material.TireWeight,
            MaterialHeight = material.MaterialHeight,


            WinMax = material.Winmax,
            WinMin = material.Winmin,
            SumMax = material.Summax,
            SumMin = material.Summin,
        };


        var result = 0;
        //更新规格
        if (materialMaster is null)
        {
            prodMaterial.Id = id;
            result = db.ProdMaterials.Insert(prodMaterial);
        }
        else
        {
            result = db.ProdMaterials.Where(r => r.Id == materialMaster.Id).Update(prodMaterial);
        }

        //更新物料
        if (materialInfo is null)
        {
            prodMaterialInfo.Id = id2;
            result = db.ProdMaterialInfos.Insert(prodMaterialInfo);
        }
        else
        {
            result = db.ProdMaterialInfos.Where(r => r.Id == materialInfo.Id).Update(prodMaterialInfo);
        }
        return result;
    }

    public List<POProdMaterial> GetMaterials()
    {
        var db = Db.Default;
        var result = db.ProdMaterials.ToList();

        return result;
    }

    public List<StockMaterialCodeNumBean> GetStockMaterialCodeAndNum()
    {
        var db = Db.Default;
        var result = db.VStockMaterialcodeNums.Select(s => new StockMaterialCodeNumBean
        {
            MaterialCode = s.MaterialCode,
            Num = s.Num ?? 0,
            LockQuantity = s.LockQuantity ?? 0,
        }).ToList();
        return result;

        // var result = db.WcsLocSkus.Join(db.RcsLocations, f => f.LocId, s => s.Id, (f, s) => new
        // {
        //     f.SkuId,
        //     s.LocHasSku,
        //     s.LocType,
        //     s.LocEnable,
        // }).Where(t => t.LocHasSku == 1 && t.LocType == (int)EnumLocType.Location && t.LocEnable == 1)
        // .Join(db.ProdSkus, f => f.SkuId, s => s.Id, (f, s) => new
        // {
        //     s.MaterialId
        // })
        //.Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
        //{
        //    s.MaterialCode
        //}).GroupBy(t => t.MaterialCode)
        //.Select(s => new StockMaterialCodeNumBean
        //{
        //    MaterialCode = s.Key,
        //    Num = s.Count()
        //}).ToList();
    }
}
